package com.youi.web;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.google.common.collect.Lists;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.entity.Merchant;
import com.youi.entity.Merchantwithdraw;
import com.youi.entity.Merchantwithdrawdetail;
import com.youi.entity.Ordergoods;
import com.youi.entity.Sysuser;
import com.youi.entity.Tremittance;
import com.youi.entity.Userorder;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.service.MerchantService;
import com.youi.service.MerchantwithdrawService;
import com.youi.service.MerchantwithdrawdetailService;
import com.youi.service.OrdergoodsService;
import com.youi.service.TremittanceService;
import com.youi.service.UserorderService;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.EasyPoiUtil;
import com.youi.util.ExcelStyleUtil;
import com.youi.util.JsonUtil;
import com.youi.util.LogUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商提现申请表（对账单）控制层
 *
 * @author Administrator
 */
@Controller
public class MerchantwithdrawController extends HttpRequestManage {

    @Autowired
    private MerchantwithdrawService merchantwithdrawService;
    @Autowired
    private MerchantwithdrawdetailService merchantwithdrawdetailService;
    @Autowired
    private UserorderService userorderService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private OrdergoodsService ordergoodsService;
    @Autowired
    private TremittanceService tremittanceService;


    /**
     * I-93
     * 生成对账单
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/addMerchantwithdraw")
    public String addMerchantwithdraw(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
        Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
        DecimalFormat amount = new DecimalFormat("0.00");
        String ids = getString("ids", request);
//			Map<String, Object> params=new HashMap<String, Object>();
//			params.put(Common.DELSTATUS, DelStatus.NODEL);
//			params.put(Common.ID, idsList);
//			List<Userorder> userorderList= userorderService.getByParams(params);			
        Integer status = optInteger(Common.STATUS, request);
        String des = optString("des", request, Common.DESC);
        String pms = optString("pms", request);
        String start = optString("start", request);
        String end = optString("end", request);
        Map<String, Object> prams = new HashMap<String, Object>();
        prams.put(Common.DELSTATUS, DelStatus.NODEL);
        prams.put(Common.STATUS, Arrays.asList(1, 2, 3, 4, 5));
        prams.put("refund", -1);
        if (sysuser.getMerchantId() != null) {
            prams.put("merchantId", sysuser.getMerchantId());
        }
        if (status != null) {
            prams.put("merchantStatus", status);
        }
        Map<String, Object> sortPram = new HashMap<String, Object>();
        sortPram.put(Common.ID, des);
        Map<String, Object> noParams = null;
        if (ids != null && ids.trim().length() > 0) {
            List<Long> idsList = ObjectUtil.getIdListByIds(ids);
            if (idsList == null || idsList.size() == 0) {
                return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
            }
            noParams = new HashMap<String, Object>();
            noParams.put(Common.ID, idsList);
        }
        //供应商编码/供应商编号
        Map<String, Object> searchPram = null;
        if (pms != null && pms.length() > 0) {
            searchPram = new HashMap<String, Object>();
            searchPram.put("merchantCode", pms);
            searchPram.put("merchantName", pms);
        }
        Map<String, Object> startTime = null;
        if (start != null) {
            startTime = new HashMap<String, Object>();
            startTime.put("gmtOrder", DateUtil.simpdfyMdHms.parse(start));
        }
        Map<String, Object> endTime = null;
        if (end != null) {
            endTime = new HashMap<String, Object>();
            endTime.put("gmtOrder", DateUtil.simpdfyMdHms.parse(end));
        }
        List<Userorder> userorderList = userorderService.getUserorderByNoIds(prams, noParams, searchPram, sortPram, startTime, endTime);
        if (userorderList == null || userorderList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        userorderService.getWithdrawUserorderMsg(userorderList);
        List<Long> merchantId = new ArrayList<Long>();
        Double merchantAmount = 0.00;
        Double orderAmount = 0.00;
        Double platformAmount = 0.00;
        for (Userorder userorder : userorderList) {
            if (userorder.getMerchantStatus() != 1 && userorder.getMerchantStatus() != 4) {
                return JsonUtil.toJson(CommonInfoUtil.NOOPPMS, "选择的订单非可提现状态无法生成对账单");
            }
            if (userorder.getRefund() != -1) {
                return JsonUtil.toJson(CommonInfoUtil.NOOPPMS, "选择的订单已退款无法生成对账单");
            }
            merchantId.add(userorder.getMerchantId());
            if (userorder.getMerchantAmount() != null) {
                merchantAmount += userorder.getMerchantAmount();
            }
            if (userorder.getPlatformAmount() != null) {
                platformAmount += userorder.getPlatformAmount();
            }
            if (userorder.getAmount() != null) {
                orderAmount += userorder.getAmount();
            }
            userorder.setMerchantStatus(2);
            LogUtil.Info("=========生成对账单的单号:" + userorder.getOrderNo());
        }
        if (ObjectUtil.removeDuplicate(merchantId).size() > 1) {
            System.out.println(merchantId.toString());
            return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "选择的订单不为同一供应商无法生成对账单");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put(Common.ID, merchantId);
        Merchant merchant = merchantService.getByParam(params);
        if (merchant == null) {
            return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "供应商不存在");
        }
        Double merchanRatio = 100.00 - merchant.getCommissionRatio();
        Merchantwithdraw merchantwithdraw = new Merchantwithdraw(sysuser.getId(), merchant.getId(), merchant.getMerchantCode(), merchant.getName());
        merchantwithdrawService.saveOrUpdate(merchantwithdraw);
        int j = 1;
        Userorder userorder = null;
        for (int i = 0; i < userorderList.size(); i++) {
            userorder = userorderList.get(i);
            if (userorder.getOrdergoodsList() != null && userorder.getOrdergoodsList().size() > 0) {
                ObjectUtil.ToChangeNullToEmpty(userorder);
                params = new HashMap<String, Object>();
                params.put(Common.DELSTATUS, DelStatus.NODEL);
                params.put("orderId", userorder.getId());
                List<Ordergoods> ordergoodList = ordergoodsService.getByParams(params);
                if (ordergoodList == null || ordergoodList.size() == 0) {
                    continue;
                }
                String goodsName = null;
                String goodsPrice = null;
                String goodsCount = null;
                StringBuffer sb1 = new StringBuffer();
                StringBuffer sb2 = new StringBuffer();
                StringBuffer sb3 = new StringBuffer();
                for (Ordergoods ordergoods : ordergoodList) {
                    sb1.append(ordergoods.getName()).append(";");
                    sb2.append(ordergoods.getSlaePrice().toString()).append(";");
                    sb3.append(ordergoods.getCount().toString()).append(";");
                }
                goodsName = sb1.deleteCharAt(sb1.length() - 1).toString();
                goodsPrice = sb2.deleteCharAt(sb2.length() - 1).toString();
                goodsCount = sb3.deleteCharAt(sb3.length() - 1).toString();
                Merchantwithdrawdetail merchantwithdrawdetail = new Merchantwithdrawdetail(merchantwithdraw.getId());
                merchantwithdrawdetail.setMerchantStatus(userorder.getMerchantStatus());
                merchantwithdrawdetail.setOrderStatus(userorder.getStatus());
                merchantwithdrawdetail.setWithdrawNo(merchantwithdraw.getTwithdrawNo());
                merchantwithdrawdetail.setDateNo(merchantwithdraw.getDateNo());
                merchantwithdrawdetail.setOrderNo(userorder.getOrderNo());
                merchantwithdrawdetail.setGoodsName(goodsName);
                merchantwithdrawdetail.setGoodsPrice(goodsPrice);
                merchantwithdrawdetail.setGoodsCount(goodsCount);
                merchantwithdrawdetail.setOrderAmount(userorder.getAmount());
                merchantwithdrawdetail.setMerchantName(merchant.getName());
                merchantwithdrawdetail.setMerchantCode(merchant.getMerchantCode());
                merchantwithdrawdetail.setMerchantAmount(userorder.getMerchantAmount());
                merchantwithdrawdetail.setMerchanRatio(Double.valueOf(amount.format(merchanRatio)));
                merchantwithdrawdetail.setGmtOrder(userorder.getGmtOrder());
                if (userorder.getGmtComplete() != null) {
                    merchantwithdrawdetail.setGmtComplete(userorder.getGmtComplete());
                }
                merchantwithdrawdetailService.saveOrUpdate(merchantwithdrawdetail);
            }
        }
        merchantwithdraw.setOrderCount(userorderList.size());
        merchantwithdraw.setOrderAmount(orderAmount);
        merchantwithdraw.setMerchantAmount(merchantAmount);
        merchantwithdraw.setPlatformAmount(platformAmount);
        merchantwithdraw.setProfitRatio(merchanRatio);
        merchantwithdrawService.saveOrUpdate(merchantwithdraw);
        userorderService.saveOrUpdateAll(userorderList);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
    }


    /**
     * I-94
     * 查询供应商对账单列表
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/getMerchantwithdraw")
    public String getMerchantwithdraw(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
        Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
        Integer pageIndex = optInteger("pageIndex", request, 1);
        Integer pageNum = optInteger("pageNum", request, 10);
        Integer status = optInteger(Common.STATUS, request);
        String des = optString("des", request, Common.DESC);
        String pms = optString("pms", request);
        String twithdrawNo = optString("twithdrawNo", request);
        String dateNo = optString("dateNo", request);
        Date start = optDate("start", request, "yyyy-MM-dd HH:mm:ss");
        Date end = optDate("end", request, "yyyy-MM-dd HH:mm:ss");
        Map<String, Object> prams = new HashMap<String, Object>();
        prams.put(Common.DELSTATUS, DelStatus.NODEL);
        if (sysuser.getMerchantId() != null) {
            prams.put("merchantId", sysuser.getMerchantId());
        }
        if (status != null) {
            prams.put(Common.STATUS, status);
        }
        if (twithdrawNo != null && twithdrawNo.trim().length() > 0) {
            prams.put("twithdrawNo", twithdrawNo);
        }
        if (dateNo != null && dateNo.trim().length() > 0) {
            prams.put("dateNo", dateNo);
        }
        Map<String, Object> sortPram = new HashMap<String, Object>();
        sortPram.put(Common.ID, des);
        Map<String, Object> searchPram = null;
        if (pms != null) {
            searchPram = new HashMap<String, Object>();
            searchPram.put("merchantCode", pms);
            searchPram.put("merchantName", pms);
        }
        Map<String, Object> startTime = null;
        if (start != null) {
            startTime = new HashMap<String, Object>();
            startTime.put(Common.GMTCREATED, start);
        }
        Map<String, Object> endTime = null;
        if (end != null) {
            endTime = new HashMap<String, Object>();
            endTime.put(Common.GMTCREATED, end);
        }
        List<Merchantwithdraw> merchantwithdrawList = merchantwithdrawService.getByParamsList(prams, searchPram, sortPram, (pageIndex - 1) * pageNum, pageNum, startTime, endTime);
        if (merchantwithdrawList == null || merchantwithdrawList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        Integer pageCount = merchantwithdrawService.getCountByParamss(prams, searchPram, startTime, endTime);
        modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
        modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);            //pageIndex     查询页数
        modelMap.put(CommonInfoUtil.ALLCOUINT, pageCount);
        modelMap.put(CommonInfoUtil.DATA, merchantwithdrawList);        //jsonObjectList json对象集合
        merchantwithdrawService.getMerchantwithdrawMsg(merchantwithdrawList);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", modelMap);
    }

    /**
     * I-96
     * 平台导出供应商对账单excel
     */
    @Deprecated
    @ResponseBody
    @RequestMapping("platformToExcelMerchantwithdraw")
    public String toExcelMerchantwithdraw(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
        Integer status = optInteger(Common.STATUS, request);
        String des = optString("des", request, Common.DESC);
        String pms = optString("pms", request);
        String twithdrawNo = optString("twithdrawNo", request);
        String dateNo = optString("dateNo", request);
        Date start = optDate("start", request, "yyyy-MM-dd HH:mm:ss");
        Date end = optDate("end", request, "yyyy-MM-dd HH:mm:ss");
        Map<String, Object> prams = new HashMap<String, Object>();
        prams.put(Common.DELSTATUS, DelStatus.NODEL);
        if (status != null) {
            prams.put(Common.STATUS, status);
        }
        if (twithdrawNo != null && twithdrawNo.trim().length() > 0) {
            prams.put("twithdrawNo", twithdrawNo);
        }
        if (dateNo != null && dateNo.trim().length() > 0) {
            prams.put("dateNo", dateNo);
        }
        Map<String, Object> sortPram = new HashMap<String, Object>();
        sortPram.put(Common.ID, des);
        Map<String, Object> searchPram = null;
        if (pms != null) {
            searchPram = new HashMap<String, Object>();
            searchPram.put("merchantCode", pms);
            searchPram.put("merchantName", pms);
        }
        Map<String, Object> startTime = null;
        if (start != null) {
            startTime = new HashMap<String, Object>();
            startTime.put(Common.GMTCREATED, start);
        }
        Map<String, Object> endTime = null;
        if (end != null) {
            endTime = new HashMap<String, Object>();
            endTime.put(Common.GMTCREATED, end);
        }
        List<Merchantwithdraw> merchantwithdrawList = merchantwithdrawService.getByParamsAll(prams, searchPram, sortPram, startTime, endTime);
        if (merchantwithdrawList == null || merchantwithdrawList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        merchantwithdrawService.getMerchantwithdrawMsg(merchantwithdrawList);
        merchantwithdrawService.platformToExcelMerchantwithdraw(request, merchantwithdrawList);
        modelMap.put(CommonInfoUtil.JSONCODE, CommonInfoUtil.SUCCESS);
        modelMap.put(CommonInfoUtil.JSONMSG, "操作成功");
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", modelMap);
    }

    /**
     * I-97
     * 商家导出供应商对账单excel
     */
    @Deprecated
    @ResponseBody
    @RequestMapping("merchantToExcelMerchantwithdraw")
    public String merchantToExcelMerchantwithdraw(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
        Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
        Integer status = optInteger(Common.STATUS, request);
        String des = optString("des", request, Common.DESC);
        String pms = optString("pms", request);
        String twithdrawNo = optString("twithdrawNo", request);
        String dateNo = optString("dateNo", request);
        Date start = optDate("start", request, "yyyy-MM-dd HH:mm:ss");
        Date end = optDate("end", request, "yyyy-MM-dd HH:mm:ss");
        Map<String, Object> prams = new HashMap<String, Object>();
        prams.put(Common.DELSTATUS, DelStatus.NODEL);
        if (sysuser.getMerchantId() != null) {
            prams.put("merchantId", sysuser.getMerchantId());
        }
        if (status != null) {
            prams.put(Common.STATUS, status);
        }
        if (twithdrawNo != null && twithdrawNo.trim().length() > 0) {
            prams.put("twithdrawNo", twithdrawNo);
        }
        if (dateNo != null && dateNo.trim().length() > 0) {
            prams.put("dateNo", dateNo);
        }
        Map<String, Object> sortPram = new HashMap<String, Object>();
        sortPram.put(Common.ID, des);
        Map<String, Object> searchPram = null;
        if (pms != null) {
            searchPram = new HashMap<String, Object>();
        }
        Map<String, Object> startTime = null;
        if (start != null) {
            startTime = new HashMap<String, Object>();
            startTime.put(Common.GMTCREATED, start);
        }
        Map<String, Object> endTime = null;
        if (end != null) {
            endTime = new HashMap<String, Object>();
            endTime.put(Common.GMTCREATED, end);
        }
        List<Merchantwithdraw> merchantwithdrawList = merchantwithdrawService.getByParamsAll(prams, searchPram, sortPram, startTime, endTime);
        if (merchantwithdrawList == null || merchantwithdrawList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        merchantwithdrawService.getMerchantwithdrawMsg(merchantwithdrawList);
        merchantwithdrawService.merchantToExcelMerchantwithdraw(request, merchantwithdrawList);
        modelMap.put(CommonInfoUtil.JSONCODE, CommonInfoUtil.SUCCESS);
        modelMap.put(CommonInfoUtil.JSONMSG, "操作成功");
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", modelMap);
    }


    /**
     * 导出供应商对账单(提现单),包括平台和供应商自己
     *
     * @param param
     * @param request
     * @param response
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "sysUserGetInfo/exportMerchantWithdraw", produces = "application/json")
    public void exportMerchantWithdraw(@RequestBody Map<String, Object> param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
        //商户账号不需要根据自己的商户编号或名称查询
        if (sysuser.getType() == 1) {
            param.remove("pms");
        }
        //根据条件查询全部数据,不分页
        List<Map<String, Object>> exportOrderList = merchantwithdrawService.listMerchantWithdrawData(param);
        if (CollectionUtils.isEmpty(exportOrderList)) {
            throw new BusinessException(-1, "没有要导出的数据");
        }

        try {
            String fileName = "供应商对账单" + DateUtil.simpyMdHHmmss.format(new Date());
            ExportParams exportParams = new ExportParams(null, "对账单", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            //平台
            if (sysuser.getType() == 0) {
                EasyPoiUtil.exportExcel(fileName, exportParams, getExcelExportEntityForPlatform(), exportOrderList, request, response);
            }
            //商户
            if (sysuser.getType() == 1) {
                EasyPoiUtil.exportExcel(fileName, exportParams, getExcelExportEntityForMerchant(), exportOrderList, request, response);
            }
        } catch (Exception ex) {
            throw new BusinessException(-1, "导出数据失败");
        }
    }

    /**
     * 平台导出供应商提现字段
     *
     * @return
     */
    private List<ExcelExportEntity> getExcelExportEntityForPlatform() {
        List<ExcelExportEntity> entity = Lists.newArrayList();
        entity.add(new ExcelExportEntity("提现单号", "twithdrawNo", 20));
        entity.add(new ExcelExportEntity("期号", "dateNo", 15));
        entity.add(new ExcelExportEntity("供应商编号", "merchantCode", 10));
        entity.add(new ExcelExportEntity("供应商名称", "merchantName", 25));
        entity.add(new ExcelExportEntity("订单总金额", "orderAmount", 15));
        entity.add(new ExcelExportEntity("订单总数", "orderCount", 10));
        entity.add(new ExcelExportEntity("平台分润总额", "platformAmount", 15));
        entity.add(new ExcelExportEntity("供应商分润总额", "merchantAmount", 15));
        entity.add(new ExcelExportEntity("供应商分润比例", "profitRatio", 10));
        entity.add(new ExcelExportEntity("提现方式", "withdrawType", 15));
        entity.add(new ExcelExportEntity("提现状态", "statusTxt", 10));
        entity.add(new ExcelExportEntity("供应商审核状态", "merchantStatusTxt", 10));
        entity.add(new ExcelExportEntity("供应商审核时间", "gmtMerchantApply", 20));
        entity.add(new ExcelExportEntity("供应商审核意见", "merchantIdea", 25));
        entity.add(new ExcelExportEntity("创建人", "createName", 15));
        entity.add(new ExcelExportEntity("创建时间", "gmtCreated", 20));
        entity.add(new ExcelExportEntity("审核人", "applyUserName", 15));
        entity.add(new ExcelExportEntity("审核时间", "gmtApply", 20));
        entity.add(new ExcelExportEntity("审核意见", "platformIdea", 25));
        return entity;
    }

    /**
     * 供应商导出供应商提现字段
     *
     * @return
     */
    private List<ExcelExportEntity> getExcelExportEntityForMerchant() {
        List<ExcelExportEntity> entity = Lists.newArrayList();
        entity.add(new ExcelExportEntity("提现单号", "twithdrawNo", 20));
        entity.add(new ExcelExportEntity("期号", "dateNo", 15));
        entity.add(new ExcelExportEntity("订单总金额", "orderAmount", 15));
        entity.add(new ExcelExportEntity("订单总数", "orderCount", 10));
        entity.add(new ExcelExportEntity("订单分润", "merchantAmount", 15));
        entity.add(new ExcelExportEntity("分润比例", "profitRatio", 10));
        entity.add(new ExcelExportEntity("提现方式", "withdrawType", 15));
        entity.add(new ExcelExportEntity("提现状态", "statusTxt", 10));
        entity.add(new ExcelExportEntity("平台审核状态", "platformStatusTxt", 10));
        entity.add(new ExcelExportEntity("平台审核时间", "gmtApply", 20));
        entity.add(new ExcelExportEntity("平台审核意见", "platformIdea", 25));
        return entity;
    }


    /**
     * I-98
     * 供应商审核对账单
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/merchantApplyMerchantwithdraw")
    public String merchantApplyMerchantwithdraw(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
        String ids = getString("ids", request);
        Integer status = getInteger(Common.STATUS, request);
        String idea = optString("idea", request);
        List<Long> idsList = ObjectUtil.getIdListByIds(ids);
        if (idsList == null || idsList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put(Common.ID, idsList);
        List<Merchantwithdraw> merchantwithdrawList = merchantwithdrawService.getByParams(params);
        if (merchantwithdrawList == null || merchantwithdrawList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "对账单不存在");
        }
        for (Merchantwithdraw merchantwithdraw : merchantwithdrawList) {
            if (merchantwithdraw.getMerchantStatus() != 0) {
                return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "无法重复审核提现申请");
            }
            if (status == -1) {
                merchantwithdraw.setStatus(-1);
                Integer result = merchantwithdrawService.updateUserordeMerchantWithdraw(merchantwithdraw, status, 0);
                if (result == 0) {
                    return JsonUtil.toJson(CommonInfoUtil.ISLOCK, "对账单内订单不存在");
                }
            } else if (status == 1) {
                merchantwithdraw.setStatus(1);
            }
            if (idea != null && idea.trim().length() > 0) {
                merchantwithdraw.setMerchantIdea(idea);
            }
            merchantwithdraw.setMerchantStatus(status);
            merchantwithdraw.setGmtMerchantApply(new Date());
            merchantwithdrawService.saveOrUpdate(merchantwithdraw);
        }
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
    }

    /**
     * I-98-1
     * 平台审核对账单
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/platformApplyMerchantwithdraw")
    public String platformApplyMerchantwithdraw(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
        Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
        String ids = getString("ids", request);
        Integer status = getInteger(Common.STATUS, request);
        String idea = optString("idea", request);
        List<Long> idsList = ObjectUtil.getIdListByIds(ids);
        if (idsList == null || idsList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put(Common.ID, idsList);
        List<Merchantwithdraw> merchantwithdrawList = merchantwithdrawService.getByParams(params);
        if (merchantwithdrawList == null || merchantwithdrawList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "对账单不存在");
        }
        for (Merchantwithdraw merchantwithdraw : merchantwithdrawList) {
            if (merchantwithdraw.getMerchantStatus() != 1) {
                return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "供应商未审核平台无法审核对账单");
            }
            if (merchantwithdraw.getPlatformStatus() != 0) {
                return JsonUtil.toJson(CommonInfoUtil.NOOPPMS, "无法重复审核提现申请");
            }
            if (status == -1) {
                merchantwithdraw.setStatus(-1);
                Integer result = merchantwithdrawService.updateUserordeMerchantWithdraw(merchantwithdraw, status, 1);
                if (result == 0) {
                    return JsonUtil.toJson(CommonInfoUtil.ISLOCK, "对账单内订单不存在");
                }
            } else if (status == 1) {
                merchantwithdraw.setStatus(2);
                Tremittance tremittance = new Tremittance();
                tremittance.setCreatedUserId(merchantwithdraw.getCreatedId());
                tremittance.setType(0);
                tremittance.setStatus(0);
                tremittance.setPayStatus(0);
                tremittance.setWithdrawNo(merchantwithdraw.getTwithdrawNo());
                tremittance.setOrderAmount(merchantwithdraw.getOrderAmount());
                tremittance.setProfitRatio(merchantwithdraw.getProfitRatio());
                tremittance.setServiceCharge(0.00);
                tremittance.setPayeeName(merchantwithdraw.getMerchantName());
                tremittance.setPayType(0);
                tremittanceService.saveOrUpdate(tremittance);
            }
            if (idea != null && idea.trim().length() > 0) {
                merchantwithdraw.setPlatformIdea(idea);
            }
            merchantwithdraw.setApplyUserId(sysuser.getId());
            merchantwithdraw.setPlatformStatus(status);
            merchantwithdraw.setGmtApply(new Date());
            merchantwithdrawService.saveOrUpdate(merchantwithdraw);
        }
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
    }
}
